/**   
* @Title: DeptDao.java 
* @Description: TODO
* @author xuelei   
* @date 2012-3-10 下午8:04:44 
* @version V1.0   
*/ 
package com.creating.mict.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.creating.mict.domain.Column;
import com.creating.mict.domain.Office;

/**
 * 
* @ClassName: OfficeDao 
* @Description: 机构Dao 
* @author xuelei
* @date May 18, 2013 12:07:33 PM 
*
 */
public class OfficeDao extends HibernateDaoSupport {

	private static final Log log = LogFactory.getLog(OfficeDao.class);
	
	/**
	 * 
	* @Title: saveOffice 
	* @Description:  
	* @param @param office
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean saveOffice(Office office) {
		try {
			
			getHibernateTemplate().saveOrUpdate(office);
			getHibernateTemplate().flush();
			log.info("......保存机构" );
			return true;
		} catch (RuntimeException re) {
			log.error("......保存机构失败！", re);
			//throw re;
			return false;
		}
	}
	
	
	public List<Office> getOfficeByUserId(String loginName) {
		String sql = "";
//		sql = " select a.fid, a.fcode, a.fname, a.fpym, a.fadd, a.fadmin, a.fadtel, a.ftel, a.femail, a.fxzqh, a.ftype, a.fleaf, a.fcheck, a.fparent, a.fstate, a.fbz, a.forder "
//			+ " from tds_office a, tds_user b, tds_user_office c " 
//			+ " where b.floginname=c.fuser and a.fid=c.foffice "
//			+ " and b.floginname='" + loginName + "' "
//			+ " order by a.fcode asc, a.forder asc, a.fname asc "; 
		
		sql = " select a.fid, a.fcode, a.fname, a.fpym, a.fadd, a.fadmin, a.fadtel, a.ftel, a.femail, a.fxzqh, a.ftype, a.fleaf, a.fcheck, a.fparent, a.fstate, a.fbz, a.forder "
			+ " from tds_office a " 
			+ " where a.fparent = '" + loginName + "' " 
			+ " order by a.forder asc, a.fname asc ";
		
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		List<Office> offi_list = new ArrayList<Office>();
		
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			Office offi = new Office();
			for (int j = 0; j < obj.length; j++) {
				offi.setFid("" + obj[0]);
				offi.setFcode("" + obj[1]);
				offi.setFname("" + obj[2]);
				offi.setFpym("" + obj[3]);
				offi.setFadd("" + obj[4]);
				offi.setFadmin("" + obj[5]);
				offi.setFadtel("" + obj[6]);
				offi.setFtel("" + obj[7]);
				offi.setFemail("" + obj[8]);
				offi.setFxzqh("" + obj[9]);
				offi.setFtype("" + obj[10]);
				offi.setFleaf("" + obj[11]);
				offi.setFcheck("" + obj[12]);
				offi.setFparent("" + obj[13]);
				offi.setFstate("" + obj[14]);
				offi.setFbz("" + obj[15]);
			}
			offi_list.add(offi);
		}
		return offi_list;
	}
	
	
	public List<Office> queryOfficeList(String node) {
		String sql = "";
		
		sql = " select a.fid, a.fcode, a.fname, a.fpym, a.fadd, a.fadmin, a.fadtel, a.ftel, a.femail, a.fxzqh, a.ftype, a.fleaf, a.fcheck, a.fparent, a.fstate, a.fbz, a.forder "
			+ " from tds_office a " 
			+ " where a.fparent = '" + node + "' "
			+ " order by a.forder asc, a.fname asc ";
		
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		List<Office> offi_list = new ArrayList<Office>();
		
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			Office offi = new Office();
			for (int j = 0; j < obj.length; j++) {
				offi.setFid("" + obj[0]);
				offi.setFcode("" + obj[1]);
				offi.setFname("" + obj[2]);
				offi.setFpym("" + obj[3]);
				offi.setFadd("" + obj[4]);
				offi.setFadmin("" + obj[5]);
				offi.setFadtel("" + obj[6]);
				offi.setFtel("" + obj[7]);
				offi.setFemail("" + obj[8]);
				offi.setFxzqh("" + obj[9]);
				offi.setFtype("" + obj[10]);
				offi.setFleaf("" + obj[11]);
				offi.setFcheck("" + obj[12]);
				offi.setFparent("" + obj[13]);
				offi.setFstate("" + obj[14]);
				offi.setFbz("" + obj[15]);
				offi.setForderStr("" + obj[16]);
			}
			offi_list.add(offi);
		}
		return offi_list;
	}
	
	
	/**
	 * 
	* @Title: getOfficeByCode 
	* @Description: 根据编号获取机构信息
	* @param @param fcode
	* @param @return 
	* @return Office    返回类型 
	* @throws
	 */
	public Office getOfficeByCode(String fcode) {
		try {
			String sql = " select a.fid, a.fcode, a.fname, a.fpym, a.fadd, a.fadmin, a.fadtel, a.ftel, a.femail, a.fxzqh, a.ftype, a.fleaf, a.fcheck, a.fparent, a.fstate, a.fbz, a.forder "
				+ " from tds_office a " 
				+ " where a.fcode = '" + fcode + "' ";
			Query query = this.getSession().createSQLQuery(sql);
			List list = query.list();
			
			// this.getSession().close();
			if (list != null && list.size() > 0) {
				Object[] obj = (Object[]) list.get(0);
				Office offi = new Office();
				for (int j = 0; j < obj.length; j++) {
					offi.setFid("" + obj[0]);
					offi.setFcode("" + obj[1]);
					offi.setFname("" + obj[2]);
					offi.setFpym("" + obj[3]);
					offi.setFadd("" + obj[4]);
					offi.setFadmin("" + obj[5]);
					offi.setFadtel("" + obj[6]);
					offi.setFtel("" + obj[7]);
					offi.setFemail("" + obj[8]);
					offi.setFxzqh("" + obj[9]);
					offi.setFtype("" + obj[10]);
					offi.setFleaf("" + obj[11]);
					offi.setFcheck("" + obj[12]);
					offi.setFparent("" + obj[13]);
					offi.setFstate("" + obj[14]);
					offi.setFbz("" + obj[15]);
					offi.setForderStr("" + obj[16]);
				}
				return offi;
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			log.error("根据编号获取机构信息失败！", re);
			throw re;
		}
	}
	
	/**
	 * 
	* @Title: deleteOfficeByCode 
	* @Description: 删除机构信息
	* @param @param fcode
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean deleteOfficeByCode(String fcode) {
		try {
			String sql = "delete tds_office " 
					   + " where fcode='" + fcode + "'";
			
			Query query = this.getSession().createSQLQuery(sql);
			int num = query.executeUpdate();
			if (num > 0) {
				return true;
			} else {
				return true;
			}

		} catch (RuntimeException re) {
			log.error("删除机构信息失败！", re);
			throw re;
		}
	}
	
}
